var service_view = require("../../service/view.js");
var service_user = require("../../service/user.js");
var service_adv = require("../../service/adv.js");
var service_common = require("../../service/common.js");
var service_util = require("../../service/util.js");
var service_version = require("../../service/version.js")
var value = "";
var shouquan = null;
Page({
  data: { 
    searchUserList2:[],
    allUserList:[],
    demandPage:1,
    oid:0,
    modal:'',
    off:false,
    searchUserList: {},
    wrq_news: 0,
    user_news: 0,
    list:[],
    demandList:[],
    allDemandList:[],
    page: 1, 
    val:"",
    focus:false,
    wrq_zan:0
  },
  onLoad: function () {
   
    wx.hideShareMenu()
  
    wx.showLoading({
      title: '加载中',
    })
    wx.showToast({
      title: '加载数据中',
      icon: 'loading',
      duration: 100000
    });
    this.onPullDownRefresh()
   
    //列表
  },
  off:function(){
      this.setData({
        off:false
      })
    },
  onShow: function(){
    var that=this
    service_version.getIsAuditing(function (res) {
      if (res.data.status == 0) {
        wx.showLoading({
          title: '加载中...',
        })
        that.setData({
          isCheck: false,
          no_data: "无数据"
        })
        setTimeout(function(){
          wx.hideLoading()
        },2000)
      } else {
        that.setData({
          isCheck: true
        })
      }
    })
    service_view.newReplyCount(this.newReplyCount);//用户收到的新回复个数

    this.User_new()
    this.wrq_zan()
  },
  // 获取后台消息
  User_new: function () {
    service_adv.getAdvList((res) => {
      var n = res.data.data.data.length
      service_adv.getSeeAdv(res => {
        this.setData({
          user_news: n - res.data.data.see_adv_list.length
        })
      })
    })
  },
  love:function(e){

      wx.navigateTo({
        url: '/pages/loveList/loveList?id=' + e.currentTarget.dataset.id,
      })
   
  },
  newReplyCount: function (res) {
    this.setData({
      wrq_news: res.data.data.new_reply_count
    })
  },
  // 获取万人圈点赞个数
  wrq_zan: function () {
    service_common.fetchData(
      service_common.getUrl("user.newLoveCount"),
      {
        openid: getApp().globalData.openid
      },
      (res) => {
        // 
        this.setData({
          wrq_zan: res.data.data.new_love_count
        })
      },
      "GET"
    )
  },
  //格式化时间
  // format:function(){
  //   var demandList = this.data.demandList;
  //   for(var i=0;i<demandList.length;i++){
  //     demandList[i].addTime = service_util.getTime(demandList[i].addTime);
  //   }
  //   this.setData({
  //     demandList: demandList
  //   })
  // },
  toPublish: function(){
    wx.navigateTo({
      url: '/pages/publish/publish',
    })
  },
  toMyPublish: function () {
    wx.navigateTo({
      url: '/pages/demand_my/demand_my',
    })
  },
  toAdv: function () {
    wx.navigateTo({
      url: '/pages/adv_list/adv_list',
    })
  },
  img_look:function(e){
    console.log(e)
    wx.previewImage({
      current: e.currentTarget.dataset.arr[e.currentTarget.dataset.index], // 当前显示图片的http链接
      urls: e.currentTarget.dataset.arr // 需要预览的图片http链接列表
    })
  },
  // play:function(e){
  //   var ctx = wx.createVideoContext(e.currentTarget.id)
  //   ctx.requestFullScreen();//进入全屏
  //   console.log(e)
  // },
  end:function(e){
    var ctx = wx.createVideoContext(e.currentTarget.id)
    ctx.exitFullScreen();//退出全屏
  },
  
  // 下拉刷新
  onPullDownRefresh: function () {
    
    this.setData({
      demandPage:1,
      allUserList:[],
      searchUserList2:[],
      allDemandList:[],
      off:false
    })
    service_view.getCollectList(this.data.page, this.showData);
    service_view.getDemandList(this.data.demandPage,this.demandData);
  },
  //触底事件
  onReachBottom: function () {
    console.log(1)
    this.data.demandPage++
    this.setData({
      demandPage: this.data.demandPage,
      allUserList: [],
      searchUserList2: [],
    })

    wx.showLoading({
      title: '加载数据中',
      mask: true
    })

    
    service_view.getCollectList(this.data.page, this.showData);
 
    service_view.getDemandList(this.data.demandPage, this.demandData);
    
  },
  //万人圈数据获取
  demandData:function(res){
    var demandList = res.data.data;
    console.log(res)
    for (var i = 0; i < demandList.length; i++) {
      // demandList[i].addTime = service_util.getTime(demandList[i].addTime) 
      this.data.allDemandList.push(demandList[i]);
    }
    this.setData({
      demandList: this.data.allDemandList
    })

    console.log(this.data.demandList)
    //更新数据
    wx.hideToast()
    wx.hideLoading();
    wx.stopPullDownRefresh();
    
  },

  //自定义显示
  showData: function (res) {
    var dataList = res.data.data.data;
    for (var i = 0; i < dataList.length; i++) {
      this.data.allUserList.push(dataList[i]);
    }
    //page++;  /*始终显示第一页的*/ 
    //更新数据
    console.log(this.data.allUserList)
    this.setData({
      searchUserList: this.data.allUserList
    })
    
    wx.stopPullDownRefresh();
  },
  allList: function (res) {
    this.data.allUserList = res.data.data;
    console.log(res.data.data)
    this.data.searchUserList2 = [];
    if (value == "") {
      this.data.searchUserList2 = this.data.allUserList;
    } else {
      for (var i = 0; i < this.data.allUserList.length; i++) {
        var nickname = this.data.allUserList[i].nickname
        var company = this.data.allUserList[i].company
        if (nickname != '' && company != '' && nickname != 'null' && company != 'null') {
          if (nickname.indexOf(value) != -1) {
            this.data.searchUserList2.push(this.data.allUserList[i]);
          }
          else if (company.indexOf(value) != -1) {
            this.data.searchUserList2.push(this.data.allUserList[i]);
          }
        }
      }
    }
    //更新数据
    this.setData({
      searchUserList: this.data.searchUserList2
    })
    wx.hideToast();
  },
  //查找名片
  searchData: function (e) {
    wx.showToast({
      title: '加载数据中',
      icon: 'loading',
      duration: 100000
    });
    this.data.allUserList = [];
    value = e.detail.value;
    service_view.getCollectList(this.data.page, this.allList, 0);
  },
  //点击查看名片
  // viewDemand: function (e) { 
  //   add_form_id(e.detail.formId)
  //   var openid = e.currentTarget.dataset.openid;
  //   wx.navigateTo({
  //     url: '/pages/demand_detail/demand_detail?openid=' + openid,
  //   })
  // },
  toDetail: function (e) {

    add_form_id(e.detail.formId)
    var openid = e.currentTarget.dataset.openid;
    wx.navigateTo({
      url: '/pages/new_demand_detail/new_demand_detail?openid=' + openid + '&id=' + e.currentTarget.id
    })
  },
  //打电话
  callPhone: function (event) {
    var phone = event.target.dataset.phone;
    if (phone != "") {
      wx.makePhoneCall({
        phoneNumber: phone,
        success: function (res) {
          // success
        }
      })
    }
  },
  
  //跳转到名片夹列表页
  toCollect: function(e){
    add_form_id(e.detail.formId)
    console.log(1111111)
    console.log(e.detail.formId)
    wx.navigateTo({
      url: '/pages/collect/collect',
    })
  },
  onShareAppMessage: function (res) {

    return {
      title: "'" + getApp().globalData.userInfo.nickname+"'"+",分享了一条动态",
      path: '/pages/demand_detail/demand_detail?id=' + res.target.id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  setLike: function (e) {
    this.setData({
      ID: e.currentTarget.dataset.id
    })
    service_view.TopicLove(e.currentTarget.dataset.id, this.stateLike);
  },
  stateLike: function (res) {
    console.log(res.data)
    var that = this
    if (res.data.code == 0) {
      wx.showToast({
        title: '点赞成功',
      })
      this.setData({
        demandPage: 1,
        allUserList: [],
        searchUserList2: [],
        allDemandList: [],
        off: false
      })
      service_view.getCollectList(this.data.page, this.showData);
      service_view.getDemandList(this.data.demandPage, this.Jub);
    } else {
      wx.showToast({
        title: '已点赞',
      })
    }
  },
  ed:function(){

  },
  Jub:function(res){
    var demandList = res.data.data;
    console.log(res)
    for (var i = 0; i < demandList.length; i++) {
      // demandList[i].addTime = service_util.getTime(demandList[i].addTime) 
      this.data.allDemandList.push(demandList[i]);
    } 
    for (let i = 0; i < this.data.allDemandList.length;i++){
      if (this.data.ID == this.data.allDemandList[i].id){
            this.setData({
              INDEX:i
            })
      }
    }
    this.setData({
      ["demandList[" + this.data.INDEX + "]"]: this.data.allDemandList[this.data.INDEX]
    })
    //更新数据
    wx.hideToast()
    wx.hideLoading();
    wx.stopPullDownRefresh();

  },

  //点击查看名片
  viewCard: function (e) {
    console.log(e)
    add_form_id(e.detail.formId);
    var openid = e.currentTarget.dataset.openid;

    wx.navigateTo({
      url: '/pages/other/other?openid=' + openid,
    })
  },

  //点击弹出操作栏 靠谱 评论 联系
  showOperation: function (e) {
    if (e.currentTarget.dataset.index==this.data.oid){
      this.setData({
        off: !this.data.off
      })
      return
    }else{
      this.setData({
        off: false
      })
    }
    this.setData({
      oid:e.currentTarget.dataset.index,
      off:true
    })
   
    
  },
  modalHide: function (e) {
    var target_id = e.target.id;
    if (target_id == '') {
      this.setData({
        modal: '',
        focus:false
      })
    }
  },
  //点击展开评论模态框
  modalShow: function (e) {
    this.setData({
      focus: true,
      modal: 'show',
      showOperation: '',
      send_id: e.currentTarget.dataset.id
      
    })
  },

  formSubmit: function (e) {

    var content = e.detail.value.content

    service_view.TopicReply(this.data.send_id, content, this.onFormSubmit);
    this.setData({
      val: ""
    })
  },
  onFormSubmit: function (res) {
    var that = this;
    if (res.data.code == 0) {
      that.setData({
        modal: '',
        showOperation: "",
        page: 1,
        replyList: [],
        reply: [],
        off: false,
      })
      wx.showToast({
        title: '评论成功',
        icon: 'success',
        duration: 1000
      })
      this.onPullDownRefresh()
    } else {
      wx.showModal({
        title: '提示',
        content: res.data.message,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
          that.setData({
            off:false
          })
        }

      })
    }
  },
})
//添加form_id
function add_form_id(form_id) {
  service_user.addFormId(form_id);
}